package com.sky.product.controller;

import com.sky.common.constant.StatusConstant;
import com.sky.common.result.Result;

import com.sky.api.domain.entity.Setmeal;
import com.sky.product.domain.vo.DishItemVO;
import com.sky.product.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userSetmealController")
@Api(tags = "C端-套餐浏览接口")
@RequestMapping("/user/setmeal")
@Slf4j
public class UserSetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 条件查询套餐列表
     *
     * @param categoryId 分类ID
     * @return 套餐列表
     */
    @GetMapping("/list")
    @ApiOperation("根据分类ID查询套餐列表")
    @Cacheable(cacheNames = "setmealCache", key = "#categoryId")    //  key: setmealCache::1
    public Result<List<Setmeal>> list(Long categoryId) {
        log.info("根据分类ID查询套餐列表, categoryId={}", categoryId);
        // 设置查询条件
        final Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(categoryId);
        //  查询起售的套餐
        setmeal.setStatus(StatusConstant.ENABLE);

        List<Setmeal> setmealList = setmealService.list(setmeal);
        return Result.success(setmealList);
    }

    /**
     * 根据套餐ID查询包含的菜品
     * @param setmealId 套餐ID
     * @return 包含的菜品列表
     **/
    @GetMapping("/dish/{id}")
    public Result<List<DishItemVO>>  dishList(@PathVariable("id") Long setmealId) {
        log.info("根据套餐ID查询包含的菜品, setmealId={}", setmealId);
        List<DishItemVO>  dishItemVOList = setmealService.getDishItemById(setmealId);
        return Result.success(dishItemVOList);
    }
}
